package com.main.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;


public class LoginInterceptor extends HandlerInterceptorAdapter {
  
  @Override
  public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
                             Object handler) throws Exception { 
       
        // 其他情况判断session中是否有key，有的话继续用户的操作
        if(request.getSession() != null && request.getSession().getAttribute("mainUser") != null) {
            return true;
        }
        
        // 获得请求路径的uri
        String uri = request.getRequestURI();
        
        // 判断路径是登出还是登录验证，是这两者之一的话执行Controller中定义的方法
        if(uri.endsWith("/login/login.do") || uri.endsWith("/login/out.do")||uri.endsWith("/login/add.do")) {
            return true;
        }

        // 最后的情况就是进入登录页面
        request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response); 
        return false;
  }
  
}